package main

import (
	"fmt"
	"strconv"
	"time"

	"mysql-read-excel/excel"
	"mysql-read-excel/model"
	"mysql-read-excel/mysql"
)

//直接传递sql进行执行, 效率偏低, 且存在sql注入问题
func main() {

	fmt.Println(12 > 5)
	fmt.Println("12" > "5")
	fmt.Println("6月" > "5月")
	fmt.Println(strconv.ParseInt("05", 10, 64))
	fmt.Println(time.Now().Format("2006-01"))
	//cmd.InitConfigAndDB()

	//ReturnMappingIntoDb()

}

// ReturnMappingIntoDb 将excel配置加载进数据库
func ReturnMappingIntoDb() {

	fileName := "file/return_mapping.xlsx"
	sheetName := "Sheet1"
	rows, err := excel.GetRows(fileName, sheetName)
	if err != nil {
		fmt.Println(err)
		return
	}

	var returnMappingList []*model.TbDeviceReturnMapping
	for idx, row := range rows {
		if idx == 0 {
			//跳过表头
			continue
		}

		if len(row) < 4 {
			fmt.Println("ERROR row: ", row)
			return
		}

		returnMappingList = append(returnMappingList, &model.TbDeviceReturnMapping{
			BG:         row[0],
			BsiGroup:   row[1],
			ReturnFlag: getReturnFlag(row[2]),
			BsiLv:      parseStrToInt(row[3]),
			BsiPath:    row[4],
		})
	}

	//每 100(可设置) 条记录插入一次
	rowAffect := mysql.Get().CreateInBatches(&returnMappingList, 100)
	fmt.Println("RowsAffected=", rowAffect.RowsAffected)
}

// 是: 1, 否: -1
func getReturnFlag(cellVal string) int {
	if cellVal == "是" {
		return 1
	} else {
		return -1
	}
}

func parseStrToInt(str string) int {
	if str != "" {
		tmpInt64, _ := strconv.ParseInt(str, 10, 64)
		return int(tmpInt64)
	}
	return 0
}
